package jsyf.oa.service.capital;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.capital.BankCardDao;
import jsyf.oa.entity.capital.BankCardEntity;
import jsyf.oa.facade.capital.BankCardFacade;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by houtailei on 2018/2/25.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class BankCardService implements BankCardFacade {
    @Autowired
    private BankCardDao bankcardDao;

    @Override
    public List<JSONObject> getBankCardList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("fkCompanyId");
        String accountApplication = jsonParams.getString("accountApplication");
        String useStatus = jsonParams.getString("useStatus");
        return bankcardDao.getBankCardList(companyId,accountApplication,useStatus);
    }
    @Override
    public String save(String jsonData) {
        List<BankCardEntity> entity = (List<BankCardEntity>) JSONArray.toCollection(JSONArray.fromObject(jsonData), BankCardEntity.class);
        for (int i = 0; i < entity.size(); i++) {
            BankCardEntity model=entity.get(i);
            bankcardDao.add(model);
        }
        return "true";
    }

    @Override
    public String updateBankCard(String jsonData){
        BankCardEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), BankCardEntity.class);
        BankCardEntity oldEntity = (BankCardEntity) bankcardDao.getIBaseEntityById(entity.getId());
        oldEntity.setBankName(entity.getBankName());
        oldEntity.setBankCard(entity.getBankCard());
        oldEntity.setInitMoney(entity.getInitMoney());
        oldEntity.setAccountApplication(entity.getAccountApplication());
        oldEntity.setUseStatus(entity.getUseStatus());

        bankcardDao.update(oldEntity);
        return entity.getId();
    }

    @Override
    public JSONObject getBankCard(String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        return bankcardDao.getJSONById(jsonObject.get("id").toString());
    }


}
